//
//  ViewController.m
//  Rock Paper Scissors
//
//  Created by iD Student on 6/24/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    choiceLabel = @"Welcome! Please make a move.";
    choiceSpot.text = choiceLabel;
    aiMove = [self determineComputerAction];
    move = YES;
    player1ImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"blank.jpg"]];
    won = 0;
    loss = 0;
    tie = 0;
    wins.text = [[NSString alloc] initWithFormat:@"%d",won];
    losses.text = [[NSString alloc] initWithFormat:@"%d",loss];
    ties.text = [[NSString alloc] initWithFormat:@"%d", tie];
    
   
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction) buttonPressed: (id) sender
{
    
  
    UIButton *choose = (UIButton*)sender;
    aiMove = [self determineComputerAction];
    player1ImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"%@.jpg", [choose currentTitle]]];
    
    player1ImageView.center = CGPointMake(64,98);
    choiceLabel = [[NSMutableString alloc] initWithFormat: @"You chose %@", [choose currentTitle]];
    choiceSpot.text = choiceLabel;
    
     if(aiMove == 1)
    {
        AIImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"rock.jpg"]];
        AIImageView.center = CGPointMake(256,98);
        aiChoice.text = @"The computer chose rock!";
    }
    else if(aiMove == 2){
        AIImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"paper.jpg"]];
        AIImageView.center = CGPointMake(256,98);
        aiChoice.text = @"The computer chose paper!";
    }
    else if(aiMove == 0){
        AIImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"scissors.jpg"]];
        AIImageView.center = CGPointMake(256,98);
        aiChoice.text = @"The computer chose scisssors!";
    } 
    
    if(player1ImageView.image == [UIImage imageNamed: [[NSString alloc] initWithFormat:@"rock.jpg"]]){
        if(aiMove == 1){
            winner.text = @"It's a tie!";
            tie += 1;
        }
        if(aiMove == 2){
            winner.text = @"The computer wins!";
            loss += 1;
        }
        if(aiMove == 0){
            winner.text = @"You win!";
            won += 1;
            
        }
    }
    else if(player1ImageView.image == [UIImage imageNamed: [[NSString alloc] initWithFormat:@"paper.jpg"]]){
        if(aiMove == 2){
            winner.text = @"It's a tie!";
            tie += 1;
        }
        if(aiMove == 0){
            winner.text = @"The computer wins!";
            loss += 1;
        }
        if(aiMove == 1){
            winner.text = @"You win!";
            won += 1;
        }
    }
    else if(player1ImageView.image == [UIImage imageNamed: [[NSString alloc] initWithFormat:@"scissors.jpg"]]){
        if(aiMove == 0){
            winner.text = @"It's a tie!";
            tie += 1;
        }
        if(aiMove == 1){
            winner.text = @"The computer wins!";
            loss += 1;
        }
        if(aiMove == 2){
            winner.text = @"You win!";
            won += 1;
        }
    }
    wins.text = [[NSString alloc] initWithFormat:@"%d",won];
    losses.text = [[NSString alloc] initWithFormat:@"%d",loss];
    ties.text = [[NSString alloc] initWithFormat:@"%d", tie];
    
    if((won+loss) ==10){
        [playAgain setTitle: (@"Play again?") forState:UIControlStateNormal];
        if(won > loss){
          
            [self gameOver: YES];
            
        }
        else if(loss > won){
            [self gameOver: NO];
          
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tie!" message:@"You tied!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
            [alert show];
            gameOver = YES;
            [rock setEnabled: NO];
            [paper setEnabled: NO];
            [scissors setEnabled: NO];
        }
    }
    
    
}
- (void) gameOver:(int)gameWinner
{
    if(gameWinner == YES)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Win!" message:@"You Win!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
        [alert show];

    }
    else if(gameWinner == NO)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Loss" message:@"You lost!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
        [alert show];

    }
    gameOver = YES;
    [rock setEnabled: NO];
    [paper setEnabled: NO];
    [scissors setEnabled: NO];
    
    
    
} 
- (int) determineComputerAction;
{
     return arc4random()%3;
    
}
- (IBAction) playAgain:(id) sender
{
        [playAgain setTitle: (@"Reset") forState:UIControlStateNormal];
        gameOver = NO;
        [rock setEnabled: YES];
        [paper setEnabled: YES];
        [scissors setEnabled: YES];
        won = 0;
        loss = 0;
        tie = 0;
        wins.text = [[NSString alloc] initWithFormat:@"%d",won];
        losses.text = [[NSString alloc] initWithFormat:@"%d",loss];
        ties.text = [[NSString alloc] initWithFormat:@"%d", tie];
        choiceLabel = @"Welcome! Please make a move.";
        choiceSpot.text = choiceLabel;
        aiMove = [self determineComputerAction];
        move = YES;
        player1ImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"blank.jpg"]];
        aiChoice.text = @" ";
        AIImageView.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"blank.jpg"]];
        winner.text = @" ";
    
}
@end
